package com.cxx.home.service.impl;

import com.cxx.framework.web.CustomException;
import com.cxx.home.dao.KpiDao;
import com.cxx.home.dto.kpi.KpiDto;
import com.cxx.home.entity.Kpi;
import com.cxx.home.mapper.KpiMapper;
import com.cxx.home.service.KpiService;
import com.cxx.home.vo.KpiQueryVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KpiServiceImpl implements KpiService {
    @Resource
    private KpiDao kpiDao;

    @Resource
    private KpiMapper kpiMapper;


    @Override
    public Boolean addKpi(KpiDto kpiDto) {
        Kpi kpi = new Kpi();
        BeanUtils.copyProperties(kpiDto, kpi);

        return kpiMapper.insert(kpi) == 1;
    }

    @Override
    public Boolean updateKpi(Long id, KpiDto kpiDto) {
        if (kpiMapper.selectById(id) == null) {
            throw new CustomException("该绩效不存在");
        }

        Kpi kpi = new Kpi();
        BeanUtils.copyProperties(kpiDto, kpi);
        kpi.setId(id);
        return kpiMapper.updateById(kpi) == 1;
    }

    @Override
    public Boolean deleteKpi(Long id) {
        if (kpiMapper.selectById(id) == null) {
            throw new CustomException("该绩效不存在");
        }

        return kpiMapper.deleteById(id) == 1;
    }

    @Override
    public List<KpiDto> queryKpi(KpiQueryVo queryVo) {
        return kpiDao.queryKpi(queryVo);
    }
}
